<!-- 我的 -->
<template>
  <div class="user">
    <!-- 头部 -->
    <div class="user_top">
      <img :src="headImg" width="70" alt="" />
      <h3 v-if="islogined">用户名</h3>
      <h3 @click="loginFn" v-else>点击登录</h3>
      <van-icon :name="islogined ? 'cross' : 'arrow'" />
    </div>

    <!-- 模态框 -->
    <div class="modal" v-show="ifShowModal">
      <div class="modal_bg" @click="ifShowModal = false"></div>
      <div class="modal_content">
          <!-- 模态框内容 -->
        <van-form @submit="onSubmit">
          <van-field
            v-model="username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px">
            <van-button round block type="info" native-type="submit"
              >提交</van-button
            >
          </div>
        </van-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "@/https/https";

export default {
  // name: '21181224User',

  data() {
    return {
      islogined: false,    //用户登录状态
      ifShowModal: false,  //模态框状态
      username: "",        //用户名
      password: "",        //密码
      headImg: require('../assets/touxiang.png'),
    };
  },

  mounted() {},

  methods: {
    loginFn() {
      // login
      // 显示模态框
      this.ifShowModal = true;
    },
    onSubmit() {
        // 点击提交
        // console.log(123);
        // 隐藏模态框
        this.ifShowModal = false;
        login({ username:this.username, pwd: this.password }).then((res) =>{
             console.log(res);
             if(res.data.errno == 0) {
                // 登陆成功
                // 将用户名 token 存到本地
                localStorage.setItem("token",res.data.data.token);
                localStorage.setItem("user",JSON.stringify(res.data.data.userInfo));

                // 切换登录状态
                this.islogined = true;
                this.username = res.data.data.userInfo.username;
                this.headImg = res.data.data.userInfo.avatar
              }
        })
     }
  },
};
</script>

<style lang='less' scoped>
.user {
  .user_top {
    background: #333;
    color: #fff;
    padding: 20px 10px;
    height: 70px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      border-radius: 50%;
      margin-right: 10px;
    }
    h3 {
      flex: 1;
    }
  }
  .modal {
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 99;
    left: 0;
    top: 0;
    .modal_bg {
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
    }
    .modal_content {
      width: 90%;
      height: 200px;
      position: absolute;
      background: #fff;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      padding-top: 40px;
    }
  }
}
</style>